/// 查询用户关系请求模型
class CheckFriendRelationshipRequest {
  final int userId;
  final int targetUserId;
  final int timestamp;

  CheckFriendRelationshipRequest({
    required this.userId,
    required this.targetUserId,
    int? timestamp,
  }) : timestamp = timestamp ?? DateTime.now().millisecondsSinceEpoch ~/ 1000;

  Map<String, dynamic> toJson() {
    return {
      'user1_id': userId.toString(),  // 🔥 修改为Android项目中使用的字段名
      'user2_id': targetUserId.toString(),  // 🔥 修改为Android项目中使用的字段名
      'timestamp': timestamp,
    };
  }
}

/// 用户关系信息模型
class FriendRelationshipInfo {
  final String relationshipType; // "friends", "pending", "none", "blocked"
  final int? friendshipId;
  final String? status;
  final int? intimacyScore;
  final String? remark;
  final String? createdAt;
  final bool? isFriend;
  final bool? hasPendingRequest;
  final bool? sentRequest;
  final bool? receivedRequest;

  FriendRelationshipInfo({
    required this.relationshipType,
    this.friendshipId,
    this.status,
    this.intimacyScore,
    this.remark,
    this.createdAt,
    this.isFriend,
    this.hasPendingRequest,
    this.sentRequest,
    this.receivedRequest,
  });

  factory FriendRelationshipInfo.fromJson(Map<String, dynamic> json) {
    return FriendRelationshipInfo(
      relationshipType: json['relationship_type'] ?? 'none',
      friendshipId: json['friendship_id'],
      status: json['status']?.toString(),
      intimacyScore: json['intimacy_score'],
      remark: json['remark'],
      createdAt: json['created_at'],
      isFriend: json['is_friend'],
      hasPendingRequest: json['has_pending_request'],
      sentRequest: json['sent_request'],
      receivedRequest: json['received_request'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'relationship_type': relationshipType,
      if (friendshipId != null) 'friendship_id': friendshipId,
      if (status != null) 'status': status,
      if (intimacyScore != null) 'intimacy_score': intimacyScore,
      if (remark != null) 'remark': remark,
      if (createdAt != null) 'created_at': createdAt,
      if (isFriend != null) 'is_friend': isFriend,
      if (hasPendingRequest != null) 'has_pending_request': hasPendingRequest,
      if (sentRequest != null) 'sent_request': sentRequest,
      if (receivedRequest != null) 'received_request': receivedRequest,
    };
  }
}

/// 查询用户关系响应模型
class CheckFriendRelationshipResponse {
  final String status;
  final String message;
  final FriendRelationshipInfo? data;

  CheckFriendRelationshipResponse({
    required this.status,
    required this.message,
    this.data,
  });

  bool get isSuccess => status == 'success';

  factory CheckFriendRelationshipResponse.fromJson(Map<String, dynamic> json) {
    // 🔥 适配实际的服务器响应格式
    FriendRelationshipInfo? relationshipInfo;
    
    if (json['status'] == 'success') {
      // 从服务器响应构建好友关系信息
      final relationship = json['relationship'] ?? 'none';
      final details = json['details'] as Map<String, dynamic>?;
      
      relationshipInfo = FriendRelationshipInfo(
        relationshipType: relationship,
        friendshipId: details?['friendship_id'],
        status: details?['status_text'] ?? details?['status']?.toString(),
        intimacyScore: details?['intimacy_score'],
        remark: details?['remark'],
        createdAt: details?['created_at'],
        isFriend: relationship == 'friends', // 🔥 根据relationship判断是否为好友
        hasPendingRequest: relationship == 'pending',
        sentRequest: null, // 服务器未返回这些字段
        receivedRequest: null,
      );
    }
    
    return CheckFriendRelationshipResponse(
      status: json['status'] ?? 'error',
      message: json['message'] ?? (json['status'] == 'success' ? '查询成功' : '未知错误'),
      data: relationshipInfo,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'status': status,
      'message': message,
      if (data != null) 'data': data!.toJson(),
    };
  }
}
